import sqlite from 'react-native-sqlite-storage';

import { ToastAndroid } from "react-native";

class Db {
    db = null;

    open () {
        this.db = sqlite.openDatabase({
            name: 'sqlite.db',
            location: 'default'
        }, () => {
            ToastAndroid.show("open db success!", ToastAndroid.SHORT);
        }, (err) => {
            ToastAndroid.show("open db error!" + err, ToastAndroid.SHORT);
        });
    }

    close () {
        if (this.db) {
            this.db.close();
        }
    }

    execute (sql, arg, callback) {
        this.db.transaction((tx) => {
            tx.executeSql(sql, arg, callback);
        });
    }
}

const db = new Db();

export default db;